import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/config/rules.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/order_detial/logic.dart';
import 'package:yp/router.dart';
import 'package:yp/services/api.dart';

class ItemPreWidget extends StatelessWidget {
  final Map<String, dynamic> orderData;
  final Map<String, dynamic> data;
  final OrderDetialLogic logic;
  var serviceFlag;

  ItemPreWidget({Key key, this.orderData, this.data, this.logic, this.serviceFlag})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 5.w),
      child: Row(
        children: [
          data['item_picture'] != ''
              ? Container(
                  width: 128.w,
                  height: 128.w,
                  child: GestureDetector(
                    onTap: () {
                      print('datadata');
                      print(data);
                      if (data['point_sale_type'] == 0) {
                        Get.toNamed(RouteConfig.goodsDetail, arguments: {
                          'itemNumId': data['item_num_id'].toString(),
                          'styleNumId': data['style_num_id'].toString(),
                          // 'shopId': SpUtil.getInt('storeShopNum').toString(),
                          'shopId': data['ec_shop_num_id'].toString(),
                          // 'ecStyleNumId': info['ec_style_num_id'].toString(),
                          'typeIndex': orderData['so_sign'] == 14 ? 1 : 0,
                        });
                      }
                      // Get.toNamed(RouteConfig.goodsDetail, arguments: {
                      //       'itemNumId':
                      //         data['item_num_id'].toString(),
                      //       'styleNumId':
                      //          data['style_num_id'].toString(),
                      //       'shopId': SpUtil.getInt('storeShopNum').toString(),
                      //       'ecStyleNumId':
                      //          data['ec_style_num_id'].toString(),
                      //     });
                    },
                    child: Image.network(
                      "${data['item_picture']}",
                      // width: 128.w,
                      // height: 128.w,
                      fit: BoxFit.fill,
                    ),
                  ),
                )
              : SizedBox(
                  width: 128.w,
                  height: 128.w,
                ),
          SizedBox(width: 20.w),
          Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    if(data['pmt_sign']==1)
                    Container(
                      margin: EdgeInsets.only(top: 6,right: 6),
                      padding: EdgeInsets.fromLTRB(8,3,8,3),
                      decoration:new BoxDecoration(
                        border: new Border.all(color: Color(0xFFFF0000), width: 0.5),
                        color: Colors.red,
                        borderRadius: new BorderRadius.circular((9.0))
                      ),
                      alignment: Alignment.center,
                      child: Text(
                        '赠品',
                        style: TextStyle(
                          fontSize: 18.sp,
                          color: Color(0xffffffff),
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        ),
                    ),
                    Text(
                      "${data['item_name']}",
                      style: TextStyle(
                        fontSize: 28.sp,
                        color: Color(0xff222222),
                        height: 2,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
                Text(
                  "${data['spec_nature_info'] ?? ''}",
                  style: TextStyle(
                    fontSize: 24.sp,
                    color: Color(0xff999999),
                    height: 2,
                  ),
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
                Row(
                  children: [
                    Row(
                      children: [
                        Text(
                          "￥${data['trade_price']}",
                          style: TextStyle(
                            fontSize: 28.sp,
                            color: Config.mainColor,
                            height: 1.6,
                          ),
                        ),
                        SizedBox(width: 10.w),
                        Text(
                          "x${data['point_sale_type'] == 1 ? data['qty'].toDouble().toStringAsFixed(2) : data['qty'].toInt()}",
                          style: TextStyle(
                            fontSize: 26.sp,
                            color: Colors.red,
                            height: 1.8,
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      width: 20.w,
                    ),
                    // Text(
                    //   "可赚:￥${data['commission_amount']}",
                    //   style: TextStyle(
                    //     fontSize: 28.sp,
                    //     color: Config.mainColor,
                    //     height: 1.6,
                    //   ),
                    // ),
                  ],
                ),
                Visibility(
                  visible: data['point_sale_type'] == 1 ? true : false,
                  child: Text(
                    "【定制安装商品，不支持售后】",
                    style: TextStyle(fontSize: 24.sp, color: Color(0xff999999)),
                  ),
                ),
              ],
            ),
          ),
          Container(
            child: Column(
              children: [
                Visibility(
                  visible: showLabel1(),
                  child: GestureDetector(
                    onTap: () {
                      if (rules('a16', data)) {
                        Get.toNamed(RouteConfig.aftersaleApply,
                                arguments: {'type':0,'isPre':logic.state.detail['is_pre_customer'],'id': logic.state.id, 'data': data})
                            .then((value) {
                          logic.onReady();
                          logic.update();
                        });
                      } else {
                        toast('您已申请退款');
                      }
                    },
                    child: Container(
                      padding:
                          EdgeInsets.only(left: 8, right: 8, bottom: 4, top: 4),
                      decoration: new BoxDecoration(
                          border: new Border.all(
                              color: Color(0xFFAAAAAA), width: 0.3),
                          borderRadius: new BorderRadius.circular((15.0))),

                      child: Center(
                        child: Text('申请退款',
                            style: TextStyle(color: Colors.grey, fontSize: 10)),
                      ),
                    ),
                  ),
                ),
                Container(
                  child: Column(
                    children: [
                      Visibility(
                        visible: rules('a17', data) &&
                                orderData['user_num_id'].toString() ==
                                    SpUtil.getInt('empeNumId').toString()
                            ? true
                            : false,
                        child: GestureDetector(
                          onTap: () {
                            print('======= 追加评论 点击事件 =======');
                            var params = {
                              'use_platform': 2,
                              'ec_shop_num_id': orderData['ec_shop_num_id'],
                              'work_flag': '1',
                              'tml_num_id': orderData['tml_num_id'], // 订单号
                              'page_size': '100000',
                              'page_num': '1',
                              'access_token': SpUtil.getString('token'),
                              'item_num_id':
                                  data['item_num_id'].toString(), // 商品id
                            };
                            EasyLoading.show();
                            Api.queryEvaluateInformation(params).listen(
                                (event) {
                              print(' 查询成功 返回结果  event = $event');
                              EasyLoading.dismiss();
                              if (event['code'] == 0) {
                                List list = event['item_infos'];
                                if (list.length > 0) {
                                  Map<String, dynamic> dataMap = {
                                    'tml_num_id':
                                        orderData['tml_num_id'], // 订单号
                                    'rate_id': list[0]['sd_sysrate_traderate']
                                        ['rate_id'],
                                    'shop_id': data['ec_shop_num_id'],
                                    'goodsUrl': data[
                                        'item_picture'], //state.commentsList[index]['item_picture'],
                                    'titleStr': data[
                                        'item_name'], //state.commentsList[index]['item_name'] ?? '',
                                  };
                                  Get.toNamed(RouteConfig.followUpEvaluation,
                                      arguments: dataMap);
                                }
                              }
                            }, onError: (error) {
                              print(' 查询成功 返回结果  error = $error');
                              EasyLoading.dismiss();
                              // state.commentsList = [];
                              // update();
                            });
                          },
                          child: Container(
                            padding: EdgeInsets.only(
                                left: 8, right: 8, bottom: 4, top: 4),
                            margin: EdgeInsets.only(top: 5),
                            decoration: new BoxDecoration(
                                border: new Border.all(
                                    color: Color(0xFFAAAAAA), width: 0.3),
                                borderRadius:
                                    new BorderRadius.circular((15.0))),
                            child: Text('追加评论',
                                style: TextStyle(
                                    color: Colors.grey, fontSize: 10)),
                          ),
                        ),
                      ),
                      if (data['comment_type'].toString() == "1" ||
                          data['comment_type'].toString() == "2")
                        GestureDetector(
                          onTap: () {
                            print('======= 查看评论 点击事件 =======');
                            Get.toNamed(
                              RouteConfig.pingJiaShaiDanDetails,
                              arguments: orderData,
                              parameters: {
                                'item_num_id': data['item_num_id'].toString()
                              }, //
                            );
                            // Get.toNamed(RouteConfig.evaluationSuccess);
                          },
                          child: Container(
                            padding: EdgeInsets.only(
                                left: 8, right: 8, bottom: 4, top: 4),
                            margin: EdgeInsets.only(top: 5),
                            alignment: Alignment.bottomRight,
                            decoration: new BoxDecoration(
                                border: new Border.all(
                                    color: Color(0xFFAAAAAA), width: 0.3),
                                borderRadius:
                                    new BorderRadius.circular((20.0))),
                            child: Text('查看评论',
                                style: TextStyle(
                                    color: Colors.grey, fontSize: 10)),
                          ),
                        )
                    ],
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  ///有定金隐藏单条申请退款按钮
  bool showLabel1() {
    if (logic.state.detail['deposit_amount'] != null && logic.state.detail['deposit_amount'] != 0) {
      return false;
    }
    return true;
  }
}
